package com.formiaomiao.demain.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.formiaomiao.demain.R;
import com.formiaomiao.demain.model.MsgListBean;
import com.formiaomiao.demain.util.UseGlideUtils;

import java.util.List;

import q.rorbin.badgeview.Badge;
import q.rorbin.badgeview.QBadgeView;

public class MsgListAdapter extends RecyclerView.Adapter<MsgListAdapter.ViewHolder> {

    Context context;
    List<MsgListBean> list;

    private onItemClickListener onItemClickListener;

    public interface onItemClickListener {
        void itemclickListener(View v, int i);

        void badgeDragListener(View v, int i);

        void deleteItemClick(View v, int i);
    }

    public void setOnItemClickListener(onItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public MsgListAdapter(Context context, List<MsgListBean> list) {
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View v = LayoutInflater.from(context).inflate(R.layout.msg_item_layout, viewGroup, false);
        ViewHolder holder = new ViewHolder(v);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, final int i) {
        UseGlideUtils.setGlide(context, list.get(i).getUserHead(), holder.msg_user_head);
        holder.tv_user_name.setText(list.get(i).getFriendremarks() == null || list.get(i).getFriendremarks().equals("") ?
                list.get(i).getNickName() : list.get(i).getFriendremarks());
        holder.tv_user_content.setText(list.get(i).getMsgContent());
        holder.tv_msg_time.setText(list.get(i).getMsgTime());
        holder.badge.setBadgeNumber(list.get(i).getMsgCount());
        holder.ll_msg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClickListener.itemclickListener(v, i);
            }
        });
        holder.badge.setOnDragStateChangedListener(new Badge.OnDragStateChangedListener() {
            @Override
            public void onDragStateChanged(int dragState, Badge badge, View targetView) {
                if (dragState == STATE_SUCCEED) {
                    onItemClickListener.badgeDragListener(targetView, i);
                }
            }
        });
        holder.btnDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClickListener.deleteItemClick(v, i);
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        LinearLayout ll_msg;
        ImageView msg_user_head;
        TextView tv_user_name, tv_user_content, tv_msg_time, tv_msg_count;
        Badge badge;
        Button btnDelete;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ll_msg = itemView.findViewById(R.id.ll_msg);
            msg_user_head = itemView.findViewById(R.id.msg_user_head);
            tv_user_name = itemView.findViewById(R.id.tv_user_name);
            tv_user_content = itemView.findViewById(R.id.tv_user_content);
            tv_msg_time = itemView.findViewById(R.id.tv_msg_time);
            tv_msg_count = itemView.findViewById(R.id.tv_msg_count);
            btnDelete = itemView.findViewById(R.id.btnDelete);
            badge = new QBadgeView(context).bindTarget(tv_msg_count);
            badge.setBadgeGravity(Gravity.CENTER);
            badge.setBadgeTextSize(14, true);
            badge.setBadgePadding(6, true);
        }
    }

}
